// 导入api接口
import { sjList, userAdd, userDelete, userList, userUpdate } from '@/api/api'
import configJs from '@/utils/configJs'
import { mapGetters, mapState } from 'vuex'
import defaultData from './data'
var data = JSON.parse(JSON.stringify(defaultData))
var adapter = {
	data,
	methods: {
		// ...mapActions(['logOut']),
		/**
		 * 初始化
		 */
		initPage(){
			//还原页面对象
			configJs.revert(data, defaultData);
		},
		
		/**
		 * 查询员工列表
		 */
		async search(val) {
			this.revertActive() // 重置选择行
			let params = {...this.userSearchObj[0]}
			typeof val != 'number' && (val = 1)
			params.page = val
			if (params.isAdmin === '') {
				delete params.isAdmin
			}
			const res = await userList(params)
			if (res.code === 0) {
			  this.page = res.data
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 刷新
		 */
		async refresh() {
			this.search()
		},

		/**
		 * 重置密码
		 */
		 async resetPw(row) {
			await this.$confirm('确认重置密码?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			 let params = {...row}
			 params.password = '123456'
			 const res = await userUpdate(params)
			 if (res.code === 0) {
				this.$message('重置密码成功')
			} else {
				this.$message(res.msg)
			}
		 },

		/**
		 * 增加
		 */
		add() {
			this.resetAdd()
			this.addVisi = true
		},

		/**
		 * 重置增加弹窗
		 */
		resetAdd() {
			this.addRecord = [{
				account:"",
				email:"",
				password:"",
				username:"",
				isAdmin:'',
				supperAdmin:'',
				remark:"",
				valid:'',
				createdBy:'',
				updatedBy:'',
				createdDate:"",
				updatedDate:"",
			}]
		},

		/**
		 * 增加点击
		 */
		async addClick() {
			const res = await userAdd(this.addRecord[0])
			if (res.code === 0) {
				this.$message('添加成功')
				this.addVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		 /**
			* 修改
		  */
		edit(user) {
			this.editRecord = {...user}
			this.editVisi = true
		},

		/**
		 * 修改点击
		 */
		async editClick() {
			let params = {...this.editRecord}
			params.password = this.editPw
			const res = await userUpdate(params)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 删除
		 */
		async deleteClick(id) {
			await this.$confirm('确认删除?', '', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
			const res = await userDelete(id)
			if (res.code === 0) {
				this.$message('修改成功')
				this.editVisi = false
				this.refresh()
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 点击行
		 */
		rowClick(row, column, e) {
		},

		/**
		 * 重置选择行
		 */
		revertActive() {
			this.activeRecord = {
				id:"",
				account:"",
				email:"",
				password:"",
				username:"",
				isAdmin:'',
				supperAdmin:'',
				remark:"",
				valid:'',
				createdBy:'',
				updatedBy:'',
				createdDate:"",
				updatedDate:"",
			}
		},

		/**
		 * 司机列表
		 */
		async getListSj() {
			const res = await sjList()
			if (res.code === 0) {
			  this.sjList = res.data.list
			} else {
				this.$message(res.msg)
			}
		}
	},
	computed: {
		...mapState({
			isAdmin: state => state.user.isAdmin,
			supperAdmin: state => state.user.supperAdmin,
			userId: state => state.user.id,
		}),
		...mapGetters(['userPageAuth']),
		notAdmin() {
			return this.isAdmin == 0 && !this.supperAdmin
		},
	}
}
export default adapter
